package com.viphuli.app.tool.handler;

import java.util.List;

import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.adapter.MemberAdapter;
import com.viphuli.app.tool.bean.page.CircleInfoPage;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.Constants.UserType;
import com.viphuli.app.tool.fragment.AccountFragment;
import com.viphuli.app.tool.fragment.CircleMemberFragment;
import com.viphuli.app.tool.fragment.CircleUserAddFragment;
import com.viphuli.app.tool.http.ApiRequest;

public class CircleMemberResponseHandler extends MyBaseHttpResponseHandler<CircleMemberFragment, CircleInfoPage> implements OnItemClickListener {

	private MemberAdapter adapter;

	@Override
	public void deal() {

		List<AccountUser> memberList = page.getCircleInfo().getMembers();
		caller.getUserList().clear();
		caller.getUserList().addAll(memberList);

		//更多选项
		AccountUser addUser = new AccountUser();
		addUser.setUid("-1");
		caller.getUserList().add(addUser);

		adapter = new MemberAdapter(caller, caller.getUserList());
		caller.setAdapter(adapter);

		caller.getMemberGirdView().setAdapter(adapter);
		caller.getMemberGirdView().setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

		final AccountUser user = caller.getUserList().get(position);

		if (user.getUid().equals("-1")) {

			CircleUserAddFragment.go(caller.getActivity());

		} else if (caller.isEdit()) {

			ViewUtils.confirm(caller.getActivity(), "确定删除？", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface arg0, int arg1) {

					AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(caller.getActivity());

					if (tokenKeeper.isLogin()) {

						RequestParams params = new RequestParams();
						params.put("uid", tokenKeeper.getOpenId());
						params.put("to_uid", user.getUid());
						params.put("circle_id", tokenKeeper.getUser().circleId());

						ApiRequest.circleUserDel.request(caller, params, HandlerType.waitting);

					}

				}
			});

		} else {

			AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(caller.getActivity());

			if (tokenKeeper.isLogin() && tokenKeeper.getUser().getUserType() == UserType.arrange_admin.getValue()) {

				String uid = user.getUid();
				AccountFragment.go(caller.getActivity(), uid);

			}
			
		}

	}

}
